<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    private static $data = [];
    private static $code = 20000;
    private static $message = 'success';
    /**
     * @param $code
     * @param $message
     * @param null $data
     * @return JsonResponse
     */
    public function json($code = null, $message = null, $data = null) : JsonResponse {
        $code       = is_null($code) ? self::$code : $code;
        $message    = is_null($message) ? self::$message : $message;
        $data       = is_null($data) ? self::$data : $data;
        return response()->json(['code'=>$code, 'msg' => $message, 'data' => $data]);
    }

    /**
     * @param $data
     * @return $this
     */
    public function data($data) : Controller{
        self::$data = $data;
        return $this;
    }

    public function success($data) {
        self::$data = $data;
        return $this->json();
    }

    public function fail($message, $code = 20001 ) {
        self::$code = $code;
        self::$message = $message;
        return $this->json();
    }
}
